<?php
namespace Tongcheng\Controller;
use Think\Controller;
class TopicController extends CommonController {
    public function index(){
        if(IS_AJAX){
            $model = M("Topic");
            $map = array();
            $map['wid'] = session('wid');

            $res['count'] = $model->where($map)->count();
            $page = $_GET['page']?$_GET['page']:1;
            $limit = $_GET['limit']?$_GET['limit']:10;
            $res['list'] = $model->where($map)->page("{$page},{$limit}")->select();

            $this->ajaxReturn($res);
        }else{
            $this->display();
        }  
    }

    public function add(){
    	if(IS_AJAX){
            $model = M("Topic");
            $new['wid'] = session('wid');
            $new['cid'] = I('post.cid');
            $new['title'] = I('post.title');
            $new['remark'] = I('post.remark');
            $new['head_img'] = I('post.head_img');
            $new['img_url'] = I('post.img_url');
            $new['goods'] = '';
            $new['create_time'] = date("Y-m-d H:i:s");
            if($model->add($new)){
                $this->success('添加成功！');
            }else{
                $this->error('网络忙，请稍后再试！');
            }
        }else{
            $eq['wid'] = session('wid');
            $eq['type'] = "shop";
            $cates = M("Cate")->where($eq)->select();
            $this->assign('cates',$cates);

            $this->display();
        }
    }

    public function edit(){
    	$model = M("Topic");
        $map['wid'] = session('wid');
        $map['id'] = $_GET['id'];
        if(IS_AJAX){
            $new['cid'] = I('post.cid');
            $new['title'] = I('post.title');
            $new['remark'] = I('post.remark');
            $new['head_img'] = I('post.head_img');
            $new['img_url'] = I('post.img_url');
            $model->where($map)->save($new);
            $this->success('修改成功！');
        }else{
            $eq['wid'] = session('wid');
            $eq['type'] = "shop";
            $cates = M("Cate")->where($eq)->select();
            $this->assign('cates',$cates);

            $vo = $model->where($map)->find();
            $this->assign('vo',$vo);
            $this->display('add');
        }
    }

    public function del(){
        $model = M("Topic");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        if($model->where($map)->delete()){
            $this->success("删除成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }
}